package com.bc004346.travelus.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.bc004346.travelus.R;
import com.bc004346.travelus.SettingsActivity;
import com.bc004346.travelus.model.domain.Passenger;

import java.util.ArrayList;

/**
 * Created by Renats on 10/27/2014.
 * This handles displaying of a standby list
 */
public class StandbyAdapter extends ArrayAdapter<Passenger> {

    private Activity mActivity;

    public StandbyAdapter(Activity activity, ArrayList<Passenger> flights) {
        super(activity, 0, flights);
        mActivity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = mActivity.getLayoutInflater()
                    .inflate(R.layout.list_item_standby, null);

        final Passenger s = getItem(position);
        TextView paxName = (TextView) convertView.findViewById(R.id.list_item_standby_pax_name);
        TextView paxSeat = (TextView) convertView.findViewById(R.id.list_item_standby_seat);
        TextView paxFlightLeg = (TextView) convertView.findViewById(R.id.list_item_standby_flight_leg);
        ToggleButton button = (ToggleButton) convertView.findViewById(R.id.list_item_standby_button);
        button.setChecked(SettingsActivity.isOnStandbyList(mActivity, s.getName()));

        paxName.setText(s.getName());
        paxSeat.setText(s.getSeat());
        if (s.getFlightLeg() != null && s.getFlightLeg().length() > 0)
            paxFlightLeg.setText(s.getFlightLeg());
        button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) SettingsActivity.addToStandbyList(mActivity, s.getName());
                else SettingsActivity.removeFromStandbyList(mActivity, s.getName());
            }
        });

        return convertView;
    }
}
